package com.fhnew.app.ui.dialogactivity;

import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.fhnew.app.R;
import com.fhnew.app.global.Constants;
import com.fhnew.app.model.CustomObserver;
import com.fhnew.app.model.internet.model.UserModel;
import com.fhnew.app.model.network.HttpJsonUtils;
import com.fhnew.app.model.network.entity.req.ResVerifyPhone;
import com.fhnew.app.model.network.entity.req.changeNikeNameBean;
import com.fhnew.app.util.SYSOUtils;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class UnbankDialog extends Activity {
    String bid;
    ImageView close;
    TextView phonenuber;
    EditText etsms;
    TextView smsendbt;
    LinearLayout smslayout;
    LinearLayout commitlayout;
    TextView unbankcancel;
    TextView unbankcommit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_unbank_dialog);
        close = findViewById(R.id.close);
        phonenuber = findViewById(R.id.phonenuber);
        etsms = findViewById(R.id.etsms);
        smsendbt = findViewById(R.id.smsendbt);
        smslayout = findViewById(R.id.smslayout);
        commitlayout = findViewById(R.id.commitlayout);
        unbankcancel = findViewById(R.id.unbankcancel);
        unbankcommit = findViewById(R.id.unbankcommit);
        bid = getIntent().getStringExtra("bid");
//        getisVerifyPhone();
        getdrawSmsCode();
        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        unbankcancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        unbankcommit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(etsms.getText().toString()) && TextUtils.isEmpty(bid)) {
                    setunBank(bid);
                } else {
                    Toast.makeText(UnbankDialog.this, "请输入验证码", Toast.LENGTH_LONG).show();
                }
            }
        });
    }


    private void getisVerifyPhone() {
        HttpJsonUtils.getHttpApiInstance()
                .getVerifyPhone(Constants.getuseAdress() + "api/m/bank/isVerifyPhone", UserModel.Token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CustomObserver<ResVerifyPhone>() {
                    @Override
                    protected void onCustomNext(ResVerifyPhone resVerifyPhone) {
                        if (resVerifyPhone.code == 200) {
                            phonenuber.setText(resVerifyPhone.data.phone + "");
                            getdrawSmsCode();
                        } else {
                            unbankcommit.setVisibility(View.INVISIBLE);
                            phonenuber.setText("系统错误，请联系客服");
                        }
                    }

                    @Override
                    protected void onSubscribeCustom(Disposable disposable) {

                    }

                    @Override
                    protected void onErrorCustom(Throwable e) {
                        SYSOUtils.sOut(e.toString());
                    }
                });
    }

    private void getdrawSmsCode() {
        HttpJsonUtils.getHttpApiInstance()
                .getdrawSmsCode(Constants.getuseAdress() + "api/m/bank/drawSmsCode", UserModel.Token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CustomObserver<changeNikeNameBean>() {
                    @Override
                    protected void onCustomNext(changeNikeNameBean resVerifyPhone) {
                        if (resVerifyPhone.getCode() == 200) {
                            //成功
                            timer.start();
                            phonenuber.setText(UserModel.user.isPhone + "");
                            Toast.makeText(UnbankDialog.this, "短信已成功发送成功，请注意查收", Toast.LENGTH_LONG).show();
                        } else {
                            timer.cancel();
                            unbankcommit.setVisibility(View.INVISIBLE);
                            phonenuber.setText("系统错误，请联系客服");
                        }
                    }

                    @Override
                    protected void onSubscribeCustom(Disposable disposable) {

                    }

                    @Override
                    protected void onErrorCustom(Throwable e) {
                        SYSOUtils.sOut(e.toString());
                    }
                });
    }

    CountDownTimer timer = new CountDownTimer(60000, 1000) {

        @Override
        public void onTick(long millisUntilFinished) {
            smsendbt.setText((millisUntilFinished / 1000) + "S后可重发");
            smsendbt.setEnabled(false);
        }

        @Override
        public void onFinish() {
            smsendbt.setEnabled(true);
            smsendbt.setText("获取验证码");
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        timer.cancel();
    }

    //
    private void setunBank(String bid) {
        HttpJsonUtils.getHttpApiInstance()
                .setunBank(Constants.getuseAdress() + "api/m/user/unBank", bid, etsms.getText().toString().trim(), UserModel.Token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CustomObserver<changeNikeNameBean>() {
                    @Override
                    protected void onCustomNext(changeNikeNameBean bankList) {
                        if (bankList.getCode() == 0) {
                            Toast.makeText(UnbankDialog.this, "解绑成功", Toast.LENGTH_LONG).show();
                            finish();
                        }else{
                            Toast.makeText(UnbankDialog.this, ""+bankList.getMsg(), Toast.LENGTH_LONG).show();
                        }
                    }

                    @Override
                    protected void onSubscribeCustom(Disposable disposable) {

                    }

                    @Override
                    protected void onErrorCustom(Throwable e) {
                        SYSOUtils.sOut(e.toString());
                    }
                });
    }
}
